﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Reactive.Linq;
using System.Windows;
using Caliburn.Micro;
using Wulong.Phone.Qiushi.i18n;
using Wulong.Phone.Qiushi.Models;

namespace Wulong.Phone.Qiushi.ViewModels
{
    public class CommentsViewModel : Screen
    {

        protected override void OnInitialize()
        {
            base.OnInitialize();
            if(_comments.Count==0)
            {
                LoadComments();
            }
        }
        public void LoadComments()
        {
            IsBusy = true;
            QiushiMapper.GetObservableComment(Id)
                .ObserveOnDispatcher()
                .Finally(() =>
                {
                    IsBusy = false;
                })
                .Subscribe(_comments.Add, err=> MessageBox.Show(AppResources.Ex_Network_Down_Message + "\r\n" + err.Message,
                                                                AppResources.Ex_Error_Title,
                                                                MessageBoxButton.OK));
        }

        private string _qiushiContent;
        public string QiushiContent
        {
            get { return _qiushiContent; }
            set
            {
                _qiushiContent = value;
            }
        }

        private string _imageUrl;
        public string ImageUrl
        {
            get { return _imageUrl; }
            set
            {
                _imageUrl = value;
            }
        }

        public int Id { get; set; }
      
        private ObservableCollection<QiushiComment> _comments = new ObservableCollection<QiushiComment>();
        public IEnumerable<QiushiComment> Comments
        {
            get { return _comments; }
        }

        private bool _isBusy;
        public bool IsBusy
        {
            get { return _isBusy; }
            set
            {
                if (_isBusy == value)
                    return;
                _isBusy = value;
                NotifyOfPropertyChange(() => IsBusy);
            }
        }

    }
}
